<?php
class AdminMain_Model extends Model {
	function __construct() {
		parent::__construct();
	}
	
	function eraseUser() {
		$id = $_POST['id'];
		$this->db->deleteLog("patient", "id = $id");
	}
	
	function removeUser() {
		$id = $_POST['id'];
		$this->db->delete("patient", "id = $id");
	}
	
	function removeUserCascade() {
		$id = $_POST['id'];
		$this->db->delete("patient", "id = $id");
		$this->db->delete("schedule", "id_patient = $id");
	}

	function removeSchedule() {
		$id = $_POST['id'];
		$this->db->delete("doctor_schedules", "id = $id");
	}

	function isDoctorErasable() {
		$id = $_GET['id'];
		$res = $this->db->select("SELECT * FROM schedule s where (s.id_doctor = $id)");
		echo json_encode($res);
	}
	
	function isSocialhealthErasable() {
		$id = $_GET['id'];
		$res = $this->db->select("SELECT * FROM schedule s where (s.id_socialhealth = $id)");
		echo json_encode($res);
	}
	
	function isSpecialityErasable() {
		$id = $_GET['id'];
		$res1 = $this->db->select("SELECT * FROM doctor_speciality s where (s.id_speciality = $id)");
		if (sizeof($res1) > 0 ) {
			echo json_encode($res1);
			return;
		}
		$res2 = $this->db->select("SELECT * FROM schedule s where (s.id_speciality = $id)");
		echo json_encode($res2);
	}

	function eraseSchedule() {
		$id = $_POST['id'];
		$this->db->delete("schedule", "id = $id");
	}

	function updateSchedule() {
		$id = $_POST['id'];
		$title = $_POST['title'];
		$start = $_POST['start'];
		$end = $_POST['end'];
		$startDate = new DateTime($start);
		$endDate = new DateTime($end);
		$this->db->update("doctor_schedules", array("title" => $title, "start" => $startDate->format("H:i:s"),
			"end" => $endDate->format("H:i:s"), "day" => $endDate->format("N")), "id = $id");
	}

	function insertSchedule() {
		$id = $_POST['id'];
		$title = $_POST['title'];
		$since = $_POST['start'];
		$until = $_POST['until'];
		$speciality = $_POST['speciality'];
		$day = $_POST['day'];
		$this->db->insert("doctor_schedules", array("id_doctor" => $id, "title" => $title, "start" => $since,
			"end" => $until, "day" => $day, "id_speciality" => $speciality));
	}
	
	function inserSpeciality() {
		$text = $_POST['text'];
		$temp = $this->db->select("select * from speciality s where (s.text = '$text')");
		if (sizeof($temp) > 0) {
			$this->db->update("speciality", array("text"=>$text,"enable"=>"1"), "text = '$text'");
		} else {
			$this->db->insert("speciality", array("text" => $text, "enable" => "1"));
		}
	}

	function inserSocialhealth() {
		$text = $_POST['text'];
		$temp = $this->db->select("select * from socialhealth s where (s.text = '$text')");
		if (sizeof($temp) > 0) {
			$this->db->update("socialhealth", array("text"=>$text,"enable"=>"1"), "text = '$text'");
		} else {
			$this->db->insert("socialhealth", array("text" => $text, "enable" => "1"));
		}
	}

	function restoreSocialhealth() {
		$id = $_POST['id'];
		$this->db->update("socialhealth", array("enable" => "1"), "id = $id");
	}
	
	function removeSocialhealth() {
		$id = $_POST['id'];
		$this->db->delete("socialhealth", "id = $id", 1);
	}

	function eraseSocialhealth() {
		$id = $_POST['id'];
		$this->db->update("socialhealth", array("enable" => "0"), "id = $id");
	}

	function restoreSpeciality() {
		$id = $_POST['id'];
		$this->db->update("speciality", array("enable" => "1"), "id = $id");
	}
	
	function removeSpeciality() {
		$id = $_POST['id'];
		$this->db->delete("speciality", "id = $id", 1);
	}
	
	function eraseSpeciality() {
		$id = $_POST['id'];
		$this->db->update("speciality", array("enable" => "0"), "id = $id");
	}

	function eraseLicense() {
		$id = $_POST['id'];
		$this->db->delete("license", "id = $id", 1);
	}

	function alreadyHaveLicense() {
		$id_doctor = $_GET['id_doctor'];
		$result = $this->db->select("SELECT * FROM license where (until > CURRENT_DATE and id_doctor = $id_doctor and erased = 0)");
		echo json_encode($result);
	}

	function inserLicense() {
		$id_doctor = $_POST['id_doctor'];
		$reason = $_POST['reason'];
		$since = $_POST['since'];
		$until = $_POST['until'];
		$this->db->insert("license", array("id_doctor" => $id_doctor, "reason" => $reason,
			"since" => $reason, "since" => $since, "until" => $until));
	}

	function modifyDoctor() {
		$id = $_POST['id'];
		$name = $_POST['name'];
		$lastname = $_POST['lastname'];
		$dni = $_POST['dni'];
		$dni_type = $_POST['dni_type'];
		$birthdate = $_POST['birthdate'];
		$socialhealth = $_POST['socialhealth'];
		$speciality = $_POST['speciality'];
		$this->db->delete("doctor_speciality", "doctor_speciality.id_doctor = '".$id."'", 1000);
		$this->db->delete("doctor_socialhealth", "doctor_socialhealth.id_doctor = '".$id."'", 1000);
		$this->db->update('doctor',array('name' => $name, 'lastname' => $lastname, 'birth' => $birthdate,
			'dni' => $dni, 'dni_type' => $dni_type),"id = '".$id."'");
		foreach (explode(',', $speciality) as $key => $value) {
			$this -> db -> insert('doctor_speciality', array('id_doctor' => $id, 'id_speciality' => $value));
		}
		foreach (explode(',', $socialhealth) as $key => $value) {
			$this -> db -> insert('doctor_socialhealth', array('id_doctor' => $id, 'id_socialhealth' => $value));
		}
	}

	function getDoctorById() {
		$id_get = $_GET["id"];
		$result = $this -> db -> select("SELECT * FROM doctor where (doctor.id = $id_get)");
		foreach ($result as $key => $value) {
			$id = $value["id"];
			$speciality = $this -> db -> select("SELECT s.id, s.text as text from speciality s, doctor_speciality ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_speciality)");
			$socialhealth = $this -> db -> select("SELECT s.id, s.text as text from socialhealth s, doctor_socialhealth ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_socialhealth)");
			$result[$key]['speciality'] = $speciality;
			$result[$key]['socialhealth'] = $socialhealth;
		}
		echo json_encode($result);
	}

	function restoreUser() {
		$id = $_POST["id"];
		$this -> db -> restoreLog("patient", "id = $id");
	}

	function restoreBoss() {
		$id = $_POST["id"];
		$this -> db -> restoreLog("boss", "userid = $id");
		$this -> db -> restoreLog("user", "userid = $id");
	}

	function removeBoss() {
		$id = $_POST["id"];
		$this -> db -> delete("boss", "userid = $id");
		$this -> db -> delete("user", "userid = $id");
	}

	function eraseBoss() {
		$id = $_POST["id"];
		$this -> db -> deleteLog("boss", "userid = $id");
		$this -> db -> deleteLog("user", "userid = $id");
	}
	
	function removeSecretary() {
		$id = $_POST["id"];
		$this -> db -> delete("secretary", "userid = $id");
		$this -> db -> delete("user", "userid = $id");
	}
	
	function eraseSecretary() {
		$id = $_POST["id"];
		$this -> db -> deleteLog("secretary", "userid = $id");
		$this -> db -> deleteLog("user", "userid = $id");
	}
	
	function restoreSecretary() {
		$id = $_POST["id"];
		$this -> db -> restoreLog("secretary", "userid = $id");
		$this -> db -> restoreLog("user", "userid = $id");
	}

	function eraseDoctor() {
		$id = $_POST["id"];
		$this -> db -> deleteLog("doctor", "id = $id");
		$this -> db -> deleteLog("doctor_speciality", "id_doctor = $id", 1000);
		$this -> db -> deleteLog("doctor_socialhealth", "id_doctor = $id", 1000);
		$this -> db -> deleteLog("license", "id_doctor = $id", 1000);
	}
	
	function removeDoctor() {
		$id = $_POST["id"];
		$this -> db -> delete("doctor", "id = $id");
		$this -> db -> delete("doctor_speciality", "id_doctor = $id", 1000);
		$this -> db -> delete("doctor_socialhealth", "id_doctor = $id", 1000);
		$this -> db -> delete("license", "id_doctor = $id", 1000);
	}
	
	function restoreDoctor() {
		$id = $_POST["id"];
		$this -> db -> restoreLog("doctor", "id = $id");
		$this -> db -> restoreLog("doctor_speciality", "id_doctor = $id", 1000);
		$this -> db -> restoreLog("doctor_socialhealth", "id_doctor = $id", 1000);
		$this -> db -> restoreLog("license", "id_doctor = $id", 1000);
	}

	function getLicenseForDoctorDt() {
		$id = $_GET["id"];
		$result = $this->db->select("SELECT * FROM license l where (id_doctor = $id)");
		$dt = array("aaData" => $result);
		echo json_encode($dt);
	}
	
	function getDoctorSchedules() {
		$id = $_GET['id'];
		$result = $this -> db -> select("SELECT d.id, d.title, d.start, d.end, d.day FROM doctor_schedules d where (d.id_doctor = $id)");
		foreach ($result as $key => $value) {
			
		}
		echo json_encode($result);
	}
	
	function getAllDoctorsDt() {
		$result = $this -> db -> select("SELECT * FROM doctor where erased='0'");
		foreach ($result as $key => $value) {
			$id = $value["id"];
			$speciality = $this -> db -> select("SELECT s.id, s.text as speciality from speciality s, doctor_speciality ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_speciality)");
			$socialhealth = $this -> db -> select("SELECT s.id, s.text as socialhealth from socialhealth s, doctor_socialhealth ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_socialhealth)");
			$license = $this->db->select("SELECT * FROM license l where (l.until > CURDATE() and l.id_doctor = $id)");
			$result[$key]['has_license'] = (sizeof($license)>0)?'1':'0';
			$result[$key]['speciality'] = $speciality;
			$result[$key]['socialhealth'] = $socialhealth;
		}
		$dt = array("aaData" => $result);
		echo json_encode($dt);
	}
	
	function getAllErasedDoctorsDt() {
		$result = $this -> db -> select("SELECT * FROM doctor where erased='1'");
		foreach ($result as $key => $value) {
			$id = $value["id"];
			$speciality = $this -> db -> select("SELECT s.id, s.text as speciality from speciality s, doctor_speciality ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_speciality)");
			$socialhealth = $this -> db -> select("SELECT s.id, s.text as socialhealth from socialhealth s, doctor_socialhealth ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_socialhealth)");
			$license = $this->db->select("SELECT * FROM license l where (l.until > CURDATE() and l.id_doctor = $id)");
			$result[$key]['has_license'] = (sizeof($license)>0)?'1':'0';
			$result[$key]['speciality'] = $speciality;
			$result[$key]['socialhealth'] = $socialhealth;
		}
		$dt = array("aaData" => $result);
		echo json_encode($dt);
	}
	
	function getAllDoctorsRaw() {
		$result = $this -> db -> select("SELECT * FROM doctor");
		foreach ($result as $key => $value) {
			$id = $value["id"];
			$speciality = $this -> db -> select("SELECT s.id, s.text as speciality from speciality s, doctor_speciality ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_speciality)");
			$socialhealth = $this -> db -> select("SELECT s.id, s.text as socialhealth from socialhealth s, doctor_socialhealth ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_socialhealth)");
			$license = $this->db->select("SELECT * FROM license l where (l.until > CURDATE() and l.id_doctor = $id)");
			$result[$key]['has_license'] = (sizeof($license)>0)?'1':'0';
			$result[$key]['speciality'] = $speciality;
			$result[$key]['socialhealth'] = $socialhealth;
		}
		echo json_encode($result);
	}

	function getAllDoctors() {
		$result = $this -> db -> select("SELECT * FROM doctor where erased='0'");
		foreach ($result as $key => $value) {
			$id = $value["id"];
			$speciality = $this -> db -> select("SELECT s.id, s.text as speciality from speciality s, doctor_speciality ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_speciality)");
			$socialhealth = $this -> db -> select("SELECT s.id, s.text as socialhealth from socialhealth s, doctor_socialhealth ds where (ds.id_doctor = '" . $id . "' and s.id = ds.id_socialhealth)");
			$license = $this->db->select("SELECT * FROM license l where (l.until > CURDATE() and l.id_doctor = $id)");
			$result[$key]['has_license'] = (sizeof($license)>0)?'1':'0';
			$result[$key]['speciality'] = $speciality;
			$result[$key]['socialhealth'] = $socialhealth;
		}
		echo json_encode($result);
	}
	
	function getAllErasedBossDt() {
		$result = $this -> db -> select("SELECT s.userid as id, s.name, s.lastname, u.login, s.password FROM boss s, user u where (s.userid = u.userid and u.erased='1')");
		$dt = array("aaData" => $result);
		echo json_encode($dt);
	}
	
	function getAllBossDt() {
		$result = $this -> db -> select("SELECT s.userid as id, s.name, s.lastname, u.login, s.password FROM boss s, user u where (s.userid = u.userid and u.erased='0')");
		$dt = array("aaData" => $result);
		echo json_encode($dt);
	}

	function getAllErasedSecretaryDt() {
		$result = $this -> db -> select("SELECT s.userid as id, s.name, s.lastname, u.login, s.password FROM secretary s, user u where (s.userid = u.userid and s.erased='1')");
		$dt = array("aaData" => $result);
		echo json_encode($dt);
	}
	
	function getAllSecretarysDt() {
		$result = $this -> db -> select("SELECT s.userid as id, s.name, s.lastname, u.login, s.password FROM secretary s, user u where (s.userid = u.userid and s.erased='0')");
		$dt = array("aaData" => $result);
		echo json_encode($dt);
	}
	
	function getBossById() {
		$id = $_GET["id"];
		$result2 = $this -> db -> select("SELECT s.userid as id, s.name, s.lastname, u.login, s.password FROM boss s, user u where (s.userid = u.userid) and (u.userid = $id)");
		echo json_encode($result2);
	}

	function getSecretaryById() {
		$id = $_GET["id"];
		$result2 = $this -> db -> select("SELECT s.userid as id, s.name, s.lastname, u.login, s.password FROM secretary s, user u where (s.userid = u.userid) and (u.userid = $id)");
		echo json_encode($result2);
	}
	
	function getAllBoss() {
		$result2 = $this -> db -> select("SELECT s.userid as id, s.name, s.lastname, u.login, s.password FROM boss s, user u where (s.userid = u.userid and s.erased='0')");
		echo json_encode($result2);
	}
	
	function getAllSecretarys() {
		$result2 = $this -> db -> select("SELECT s.userid as id, s.name, s.lastname, u.login, s.password FROM secretary s, user u where (s.userid = u.userid and s.erased='0')");
		echo json_encode($result2);
	}

	function getAllErasedSocialHealthDt() {
		$result2 = $this -> db -> select("SELECT * FROM socialhealth where (enable = 0)");
		echo json_encode(array("aaData" => $result2));
	}

	function getAllSocialHealthDt() {
		$result2 = $this -> db -> select("SELECT * FROM socialhealth where (enable = 1)");
		echo json_encode(array("aaData" => $result2));
	}
	
	function isDoctorInLicense() {
		$id = $_GET['id'];
		$result2 = $this->db->select("SELECT * FROM license l, doctor d where ((d.id = $id) and (d.id = l.id_doctor) and (l.until > CURDATE()))");
		echo json_encode($result2);
	}

	function getAllSocialHealth() {
		$result2 = $this -> db -> select("SELECT * FROM socialhealth where (enable = 1)");
		echo json_encode($result2);
	}
	
	function getAllSocialHealthRaw() {
		$result2 = $this -> db -> select("SELECT * FROM socialhealth;");
		echo json_encode($result2);
	}

	function getAllSpecialitiesById() {
		$id = $_GET['id'];
		$result2 = $this -> db -> select("SELECT s.id, s.text FROM doctor_speciality d, speciality s where (d.id_doctor = " . $id . ") and (s.id = d.id_speciality)");
		echo json_encode($result2);
	}

	function getAllSocialHealthById() {
		$id = $_GET['id'];
		$result2 = $this -> db -> select("SELECT s.id, s.text FROM doctor_socialhealth d, socialhealth s where (d.id_doctor = " . $id . ") and (s.id = d.id_socialhealth)");
		echo json_encode($result2);
	}

	function alreadyExistDoctor() {
		$dni = $_GET['dni'];
		$dni_type = $_GET['dni_type'];
		$result2 = $this -> db -> select("SELECT * FROM doctor where (erased = 0 and dni = $dni and dni_type = $dni_type)");
		echo json_encode($result2);
	}

	function getAllErasedSpecialitiesDt() {
		$result2 = $this -> db -> select("SELECT * FROM speciality where (enable = 0)");
		echo json_encode(array("aaData" => $result2));
	}

	function getAllSpecialitiesDt() {
		$result2 = $this -> db -> select("SELECT * FROM speciality where (enable = 1)");
		echo json_encode(array("aaData" => $result2));
	}

	function getAllSpecialities() {
		$result2 = $this -> db -> select("SELECT * FROM speciality where (enable = 1)");
		echo json_encode($result2);
	}
	
	function getAllSpecialitiesRaw() {
		$result2 = $this -> db -> select("SELECT * FROM speciality");
		echo json_encode($result2);
	}

	function modifyBoss() {
		$id = $_POST["id"];
		$name = $_POST['name'];
		$lastname = $_POST['lastname'];
		$login = $_POST['login'];
		$password = $_POST['password'];
		$this->db->update('user', array('login' => $login, 'password' => Hash::create('sha256', $password, HASH_PASSWORD_KEY), 'role' => "jefedeturno"),
		"userid = $id");
		$this -> db -> update('boss', array('name' => $name, 'lastname' => $lastname, 'userid' => $id, 'password' => $password),
		"userid = $id");
	}

	function modifySecretary() {
		$id = $_POST["id"];
		$name = $_POST['name'];
		$lastname = $_POST['lastname'];
		$login = $_POST['login'];
		$password = $_POST['password'];
		$this->db->update('user', array('login' => $login, 'password' => Hash::create('sha256', $password, HASH_PASSWORD_KEY), 'role' => "secretaria"),
		"userid = $id");
		$this -> db -> update('secretary', array('name' => $name, 'lastname' => $lastname, 'userid' => $id, 'password' => $password),
		"userid = $id");
	}
	
	function insertBoss() {
		$name = $_POST['name'];
		$lastname = $_POST['lastname'];
		$login = $_POST['login'];
		$password = $_POST['password'];
		$idSecretaria = $this -> db -> insert('user', array('login' => $login, 'password' => Hash::create('sha256', $password, HASH_PASSWORD_KEY), 'role' => "jefedeturno"));
		$this -> db -> insert('boss', array('name' => $name, 'lastname' => $lastname, 'userid' => $idSecretaria, 'password' => $password));
	}
	
	function alreadyExistSecretary() {
		$login = $_GET['login'];
		$result = $this->db->select("select * from user where (login='$login' and role='secretaria')");
		echo json_encode($result);
	}
	
	function insertSecretary() {
		$name = $_POST['name'];
		$lastname = $_POST['lastname'];
		$login = $_POST['login'];
		$password = $_POST['password'];
		$idSecretaria = $this -> db -> insert('user', array('login' => $login, 'password' => Hash::create('sha256', $password, HASH_PASSWORD_KEY), 'role' => "secretaria"));
		$this -> db -> insert('secretary', array('name' => $name, 'lastname' => $lastname, 'userid' => $idSecretaria, 'password' => $password));
	}

	function insertDoctor() {
		$name = $_POST['name'];
		$lastname = $_POST['lastname'];
		$dni = $_POST['dni'];
		$dni_type = $_POST['dni_type'];
		$birthdate = $_POST['birthdate'];
		$socialhealth = $_POST['socialhealth'];
		$speciality = $_POST['speciality'];
		$idDoctor = $this -> db -> insert('doctor', array('name' => $name, 'lastname' => $lastname, 'birth' => $birthdate, 
		'dni' => $dni, 'dni_type' => $dni_type));
		foreach (explode(',', $speciality) as $key => $value) {
			$this -> db -> insert('doctor_speciality', array('id_doctor' => $idDoctor, 'id_speciality' => $value));
		}
		foreach (explode(',', $socialhealth) as $key => $value) {
			$this -> db -> insert('doctor_socialhealth', array('id_doctor' => $idDoctor, 'id_socialhealth' => $value));
		}
	}

}
